/*
	This file is part of the OdinMS Maple Story Server
    Copyright (C) 2008 ~ 2010 Patrick Huy <patrick.huy@frz.cc> 
                       Matthias Butz <matze@odinms.de>
                       Jan Christian Meyer <vimes@odinms.de>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License version 3
    as published by the Free Software Foundation. You may not use, modify
    or distribute this program under any other version of the
    GNU Affero General Public License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package server.maps;

import java.awt.Point;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import client.DbgPlayer;
import client.DbgAccount;
import community.DbgPartyCharacter;
import packet.creators.MainPacketCreator;
import java.lang.ref.WeakReference;

public class DbgDoor extends AbstractmapleMapObject {

    private WeakReference<DbgPlayer> owner;
    private DbgMap town;
    private DbgPortal townPortal;
    private DbgMap target;
    private Point targetPosition;

    public DbgDoor(final DbgPlayer owner, final Point targetPosition) {
	super();
	this.owner = new WeakReference<DbgPlayer>(owner);
	this.target = owner.getMap();
	this.targetPosition = targetPosition;
	setPosition(this.targetPosition);
	this.town = this.target.getReturnMap();
	this.townPortal = getFreePortal();
    }

    public DbgDoor(final DbgDoor origDoor) {
	super();
	this.owner = origDoor.owner;
	this.town = origDoor.town;
	this.townPortal = origDoor.townPortal;
	this.target = origDoor.target;
	this.targetPosition = origDoor.targetPosition;
	this.townPortal = origDoor.townPortal;
	setPosition(townPortal.getPosition());
    }

    private final DbgPortal getFreePortal() {
        DbgPlayer realOwner = owner.get();
        if (realOwner != null) {
            final List<DbgPortal> freePortals = new ArrayList<DbgPortal>();

            for (final DbgPortal port : town.getPortals()) {
                if (port.getType() == 6) {
                    freePortals.add(port);
                }
            }
            Collections.sort(freePortals, new Comparator<DbgPortal>() {

                @Override
                public final int compare(final DbgPortal o1, final DbgPortal o2) {
                    if (o1.getId() < o2.getId()) {
                        return -1;
                    } else if (o1.getId() == o2.getId()) {
                        return 0;
                    } else {
                        return 1;
                    }
                }
            });
            for (final DbgMapObject obj : town.getAllDoor()) {
                final DbgDoor door = (DbgDoor) obj;
                if (door.getOwner().getParty() != null
                        && realOwner.getParty().containsMembers(new DbgPartyCharacter(door.getOwner()))) {
                    freePortals.remove(door.getTownPortal());
                }
            }

            return freePortals.iterator().next();
        }
        return null;
    }

    @Override
    public final void sendSpawnData(final DbgAccount client) {
	if (getOwner() == null || target == null || client.getPlayer() == null) {
	    return;
	}
        if (target.getId() == client.getPlayer().getMapId() || getOwner().getId() == client.getPlayer().getId() || (getOwner() != null && getOwner().getParty() != null && client.getPlayer().getParty() != null && getOwner().getParty().getId() == client.getPlayer().getParty().getId())) {
            client.getSession().write(MainPacketCreator.spawnDoor(getOwner().getId(), target.getId() == client.getPlayer().getMapId() ? targetPosition : townPortal.getPosition(), true)); //spawnDoor always has same position.
            if (getOwner() != null && getOwner().getParty() != null && client.getPlayer().getParty() != null && (getOwner().getId() == client.getPlayer().getId() || getOwner().getParty().getId() == client.getPlayer().getParty().getId())) {
                client.getSession().write(MainPacketCreator.partyPortal(town.getId(), target.getId(), 2311002, target.getId() == client.getPlayer().getMapId() ? targetPosition : townPortal.getPosition(), true));
            }
            client.getSession().write(MainPacketCreator.spawnPortal(town.getId(), target.getId(), 2311002, target.getId() == client.getPlayer().getMapId() ? targetPosition : townPortal.getPosition()));
        }
    }


    @Override
    public final void sendDestroyData(final DbgAccount client) {
	if (client.getPlayer() == null || getOwner() == null || target == null) {
	    return;
	}
	if (target.getId() == client.getPlayer().getMapId() || getOwner().getId() == client.getPlayer().getId() || (getOwner() != null && getOwner().getParty() != null && client.getPlayer().getParty() != null && getOwner().getParty().getId() == client.getPlayer().getParty().getId())) {
            client.getSession().write(MainPacketCreator.removeDoor(getOwner().getId(), true));
            if (getOwner() != null && getOwner().getParty() != null && client.getPlayer().getParty() != null && (getOwner().getId() == client.getPlayer().getId() || getOwner().getParty().getId() == client.getPlayer().getParty().getId())) {
                client.getSession().write(MainPacketCreator.partyPortal(999999999, 999999999, 0, new Point(-1, -1), false));
            }
	    client.getSession().write(MainPacketCreator.spawnPortal(999999999, 999999999, 0, null));
        }
    }

    public final void warp(final DbgPlayer chr, final boolean toTown) {
        if (getOwner() == null) {
            return;
        }
	if (chr == getOwner() || getOwner().getParty() != null && getOwner().getParty().containsMembers(new DbgPartyCharacter(chr))) {
	    if (!toTown) {
		chr.changeMap(target, targetPosition);
	    } else {
		chr.changeMap(town, townPortal);
	    }
	} else {
	    chr.getClient().getSession().write(MainPacketCreator.resetActions());
	}
    }

    public final DbgPlayer getOwner() {
	return owner.get();
    }

    public final DbgMap getTown() {
	return town;
    }

    public final DbgPortal getTownPortal() {
	return townPortal;
    }

    public final DbgMap getTarget() {
	return target;
    }

    public final Point getTargetPosition() {
	return targetPosition;
    }

    @Override
    public final DbgMapObjectType getType() {
	return DbgMapObjectType.DOOR;
    }
}
